﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using SchoolManagementSystem.Controls;
using CoreData;
namespace SchoolManagementSystem.Details
{
    //Khóa học
    public partial class CourseUC : DetailUC
    {
        public CourseUC()
        {
            InitializeComponent();
            this.ObjectName = "khóa học";
            this.Code = "Course";
            this.Type = new Course();

            

            //masterMainUC1.Model = RegisterService.RegisterServices.Course_FeeService;
            //masterMainUC1.DetailUCControl = new Course_FeeTimeUC();
            xtraTabPage2.PageVisible = false;
        }


       // String CourseID = "1=1";
        protected override long DetailDisplay()
        {
            Course data = new Course();
            if (ID > 0)
            {
                data = (Course)RegisterService.RegisterServices.CourseService.GetByID(ID);
               // CourseID = ID.ToString(); //group.GroupGuidID;
            }
            else
            {
                var code = RegisterService.RegisterServices.CourseService.GenCodeCourse();
                data.Name = "Khóa " + code;
                data.Code = "K" + code;
            }
           
            txtcode.Text = data.Code;
            txtname.Text = data.Name;

            comboBoxEdit_Month.EditValue = data.Month;
            comboBoxEdit_Year.EditValue = data.Year;
            
           
           
            txtnote.Text = data.Description;
            ckactive.Checked = data.Active;
            //masterMainUC1.SearchString = " CourseID='" + CourseID + "'"; 
          //  masterMainUC1.DetailUCControl.SearchString = " CourseID='" + CourseID + "'";
            return ID;

        }

        protected override long Save()
        {
            Course data = new Course();
            if (ID > 0)
            {
                data = (Course)RegisterService.RegisterServices.CourseService.GetByID(ID);
            }

            data.Code = txtcode.Text; //"K" + RegisterService.RegisterServices.CourseService.GenCodeCourse();
            data.Name = txtname.Text;
            data.Description = txtnote.Text;
            data.Month = int.Parse(comboBoxEdit_Month.EditValue.ToString());
            data.Year = int.Parse(comboBoxEdit_Year.EditValue.ToString());

            data.Active = ckactive.Checked;
            
            ID = RegisterService.RegisterServices.CourseService.Save(data, ref ErrorMess);
           // masterMainUC1.DetailUCControl.SearchString = ID.ToString();
            return ID;
        }


        public override bool Delete(long SelectedID)
        {
            var check = RegisterService.RegisterServices.ClassService.GetByCourseID(SelectedID);
            if (check != null && check.Count != 0)
            {
                ErrorMess = "Vui lòng xóa các lớp học liên quan trước khi xóa khóa học này";
                return false;
            }
            else
            {
                return RegisterService.RegisterServices.CourseService.Delete(RegisterService.RegisterServices.CourseService.GetByID(SelectedID));
              //  return RegisterService.RegisterServices.CourseService.UpdateIsDelete(RegisterService.RegisterServices.CourseService.GetByID(SelectedID));
            }
        }


        private void CourseUC_Load(object sender, EventArgs e)
        {
            AddTextChanged(xtraTabPage1.Controls);
          // AddTextChanged(xtraTabPage2.Controls);
        }

        //private void radioGroup_type_EditValueChanged(object sender, EventArgs e)
        //{
        //    if (ID < 0)
        //    {
        //        if (radioGroup_type.SelectedIndex == 0)
        //        {
        //            dateEdit_EndDate.EditValue = DateTime.Now.AddMonths(2);
        //        }
        //        else
        //        {
        //            dateEdit_EndDate.EditValue = DateTime.Now.AddMonths(6);
        //        }
        //    }
        //}


    }
}
